package com.cn.ccp.action.course;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseForumBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

/**
 * 课程论坛
 * @author Administrator
 *
 */
public class CourseForumAction extends BaseAction{
	
	private List<CourseForumBean> list ;
	private List<CourseForumBean> answerlist ;
	private CourseForumBean bean ;
	
	private String title ;
	private String contents ;
	private String courseId ,selectedCourseId;
	private String id ;
	private String type ;//来自哪个页面   details
	
	
	public String showBean(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		try{
			courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		}catch(Exception e){}
		type = (String)context.getExternalContext().getRequestParameterMap().get("details");
		bean = getCourseForumService().showBean(id);
		
		return "update";
	}
	
	public String addBBS(){
		FacesContext context = FacesContext.getCurrentInstance();
		String type = (String)context.getExternalContext().getRequestParameterMap().get("type");
		courseId = (String)context.getExternalContext().getRequestParameterMap().get("courseId");
		selectedCourseId = (String)context.getExternalContext().getRequestParameterMap().get("selectedCourseId");
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("bbs-type", type);
		return "addbbs";
	}
	
	
	
	public String edit(){
		String contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		bean.setContents(contents);
		if(id == null  || id.equals(""))
			add();
		else
			update();

		if(type == null || type .equals("")){
			return "edit";
		}else
			return "details";
	}
	
	
	public String add(){
		
		FacesContext context = FacesContext.getCurrentInstance();
		contents = context.getExternalContext().getRequestParameterMap().get("content1").toString();
		if(bean.getTitle() == null || bean.getTitle().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_1")));
			return "";
		}
		if(contents == null || contents.equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		getCourseForumService().install(courseId ,bean.getTitle(), contents, CCPContants.getCurrUserId() ,1);
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "edit";
	}
	
	public String cancel(){
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		String formPage = (String) session.getAttribute("bbs-type");
		if(formPage != null  && !formPage.equals("")){
			return formPage;
		}
		return "edit";
	}
	
	
	
	
	public String update(){
		FacesContext context = FacesContext.getCurrentInstance();
		String contents = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("content1").toString();
		bean.setContents(contents);
		if(bean.getTitle() == null || bean.getTitle().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_1")));
			return "";
		}
		if(bean.getContents() == null ||  bean.getContents().equals("")){
			context.addMessage("formid", new FacesMessage(getMessages("pages_bbs_error_2")));
			return "";
		}
		getCourseForumService().update( courseId,bean.getTitle(), bean.getContents(), id);
		return "main";
	}
	

	@Override
	public void getpagedetail() {
		setItemCount(getCourseForumService().getCount(courseId));
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		list = getCourseForumService().pageQuery(start, pagesize , CCPContants.getCurrUserId() ,courseId);
		int count = pagesize ;
		if(list.size() < pagesize)
			count = list.size();
		for (int i = 0; i < count; i++) {
			CourseForumBean bean = list.get(i);
			bean.setIndex((currentpage-1)*pagesize+(i+1));
			list.set(i, bean);
		}
	}
	
	public void deleteContent(){
		FacesContext context = FacesContext.getCurrentInstance();
		id = (String)context.getExternalContext().getRequestParameterMap().get("id");
		getCourseForumService().deleteContent(id);
		getpagedetail();
	}


	public List<CourseForumBean> getList() {
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		courseId = (String) session.getAttribute("bbs-courseid");
		if(list == null || list.size() == 0)
			getpagedetail();
		return list;
	}


	public void setList(List<CourseForumBean> list) {
		this.list = list;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getContents() {
		return contents;
	}


	public void setContents(String contents) {
		this.contents = contents;
	}

	public CourseForumBean getBean() {
		if(bean == null)
			bean = new CourseForumBean();
		return bean;
	}

	public void setBean(CourseForumBean bean) {
		this.bean = bean;
	}

	public List<CourseForumBean> getAnswerlist() {
		return answerlist;
	}

	public void setAnswerlist(List<CourseForumBean> answerlist) {
		this.answerlist = answerlist;
	}



	public String getId() {
		return id;
	}



	public void setId(String id) {
		this.id = id;
	}



	public String getType() {
		return type;
	}



	public void setType(String type) {
		this.type = type;
	}



	public String getCourseId() {
		//HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		return courseId;//session.getAttribute(Contants.COURSER_ID).toString();
	}



	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}

	public String getSelectedCourseId() {
		return selectedCourseId;
	}

	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}
	
}
